package org.rudenko.horo;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DbAction {
	DbHelper db;
	private String TABLE; 
	DbAction(Context cntxt, String table){
		db = new DbHelper(cntxt);
		TABLE = "horo_"+table;
	}
	
	public String getFromDb(String params){
		String result="";
		SQLiteDatabase dbR = db.getReadableDatabase();
		Cursor c = dbR.query(TABLE,new String[] {DbHelper.PARAMS, DbHelper.HORO },"params ='"+params+"'",null, null, null, null);		
		if(c.moveToFirst() && c.getString(c.getColumnIndex(DbHelper.PARAMS)).equals(params)){
			result = c.getString(c.getColumnIndex(DbHelper.HORO));
		}
		c.close();
		db.close();
		return result;
	}
	
	public void addToDb(String params, String horo){
		if(!horo.contains("null")&&params!=null){
			SQLiteDatabase dbW = db.getWritableDatabase();		
			dbW.execSQL("INSERT INTO "+TABLE+" VALUES('"+params+"', '"+horo+"')");
			db.close();
		}
	}
	
	public String[] getDbKeys(){		
		SQLiteDatabase dbR = db.getReadableDatabase();
		Cursor c = dbR.query(TABLE,new String[] {DbHelper.PARAMS},null,null, null, null, null);
		c.moveToFirst();
		String[] keys = new String[c.getCount()];
		for(int i=0; i< keys.length ;i++){
			keys[i] = c.getString(c.getColumnIndex(DbHelper.PARAMS));
			c.moveToNext();
		}
		c.close();
		db.close();
		return keys;
	}
}
